package com.example.demo.api;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;

public class SendPrivateMsg {

    public static void SendPostRequest(String jsonInputString) {
        JSONObject jsonObject = new JSONObject(jsonInputString);
        String url = jsonObject.has("group_id")?"/send_group_msg":"/send_private_msg";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, jsonInputString);
        Request request = new Request.Builder()
                .url("http://127.0.0.1:3000"+url)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {

                System.out.println("response:"+response.body().string());
            } else {
                System.out.println("Request failed: " + response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
